// pages/category/index.js
let page = 1
const MAXLIMIT = 10

Page({
  // 跳转到详情页（保留当前页面）

  
  /**
   * 页面的初始数据
   */
  data: {
    tabList:[
      {"_id":"ed863e826881f5a10612cc3d4c9ec613","child":["全部","热血","玄幻","穿越","言情","重生","爽文","其他"],"tit":"网络小说"},
      {"_id":"ed863e826881f5a10612cc3e43e3bd07","child":["全部","科幻电影","灾难大片","爱情言情","动作大片"],"tit":"电影"},
      {"_id":"ed863e826881f5a10612cc3f70b54527","child":["全部","流行","摇滚","古典","爵士","民谣","电子","说唱"],"tit":"音乐"},
      {"_id":"ed863e826881f5a10612cc40128c4a82","child":["全部","文学","历史","哲学","科幻","悬疑","传记","科普"],"tit":"书籍"},
      {"_id":"ed863e826881f5a10612cc412852c896","child":["全部","角色扮演","射击","策略","模拟经营","竞技","休闲","冒险"],"tit":"游戏"}
    ],
    categoryList:[],
    filteredList:[],
    TabCur:0,
    active:0,
    checkIdx:0,
    srcollLeft:0,
    childTab:["全部","热血","玄幻","穿越","言情","重生","爽文","其他"]
  },
  handelTab(ev){
    let {idx} = ev.currentTarget.dataset
    let selectTab = this.data.tabList[idx]
    let {offsetLeft} = ev.currentTarget //当前被点击按钮，距离左侧的偏移量
    let {checkIdx,srcollLeft} = this.data
    if(idx>checkIdx){  //判断滑动方向，左滑
      srcollLeft = offsetLeft + 100
    }
    if(idx<checkIdx){ ////判断滑动方向，右滑
      srcollLeft = offsetLeft - 80
    }
    this.setData({checkIdx:idx})
     // 当前过滤条件
     const filteredData = this.filterFunc(this.data.categoryList);
    this.setData({
      srcollLeft,
      childTab:selectTab.child,
      filteredList:filteredData,
      active:0
    })
  },
  filterFunc(data) {
    const { checkIdx } = this.data;
    if (checkIdx === 0) {
      return data;
    } else {
      const subValue = 100 + checkIdx;
      console.log(subValue);
      return data.filter(item => item.sub === subValue);
    }
  },  
  // 子标签切换事件处理
  onChange(event) {
    this.setData({
      active: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // // 加载tab
    // let res = await getApp().db.collection('booktab').get()
    // // console.log(res);
    // this.setData(
    //   {
    //     tabList:res.data,
    //     childTab:res.data[0].child
    //   })
    // 加载列表
    let {data} = await getApp().db.collection('book').skip((page-1)*MAXLIMIT).limit(MAXLIMIT).get()
    // console.log(data);
    this.setData({categoryList:data,filteredList:data})
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    page = 1
    let {data} = await getApp().db.collection('book').skip((page-1)*MAXLIMIT).limit(MAXLIMIT).get()
    this.setData({categoryList:data,filteredList:data})
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom(){
    console.log(page);
    page++
    let {data} = await getApp().db.collection('book').skip((page-1)*MAXLIMIT).limit(MAXLIMIT).get()
    // console.log("data",data);
    if(data.length!=0){
      let list = this.data.categoryList.concat(data);
      // console.log("list数据",list);
      this.setData({categoryList:list,filteredList:this.filterFunc(list)})
    }else{
      wx.showToast({
        title: '没有更多数据了',
        icon:"none"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})